import utils.TreeNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/8 10:31
 * @description:
 */
public class Solution114 {
    public void flatten(TreeNode root) {
        if (root == null) {
            return;
        }
        flatten(root.left);
        flatten(root.right);
        TreeNode left = root.left;
        TreeNode right = root.right;
        root.right = left;
        TreeNode p = root;
        while (p.right != null) {
            p = p.right;
        }
        p.right = right;
        root.left = null;
    }

    public static void main(String[] args) {
        Solution114 solution = new Solution114();
        TreeNode root = TreeNode.buildTree(new int[]{1, 2, 5, 3, 4, -1, 6});
        solution.flatten(root);
        TreeNode.show(root);
    }
}
